<div class="row-fluid">
	<div class="span12">
		<ul class="breadcrumb">
			<li>
				<a href="index.php">
					<i class="radmin-icon radmin-home"></i>
					Dashboard
				</a>
				<span class="divider">/</span>
			</li>
			<li>
				<a href="whm-accounts.php">
					<i class="radmin-icon radmin-clipboard-2"></i>
					WHM Accounts
				</a>
				<span class="divider">/</span>
			</li>
			<li class="active">
				<i class="radmin-icon radmin-clipboard"></i>
				cPanel Accounts
			</li>
		</ul>
	</div>
</div>

<div class="row-fluid">
	<div class="span12">
		<h4 class="title"><?php $this->widget('application.components.mywidgets.WikiHelp', array('wiki' => 'cpanel_accounts_change_password')); ?> cPanel Accounts | Change Passwords</h4>
		<div class="squiggly-border"></div>

		<fieldset>
			<?php echo CHtml::dropdownList('id_cpanel_accounts', '', $cpanelaccountsDataSource, array('empty' => $cpanelaccountsCount.' Accounts to Update')); ?><br />
			
			<?php echo CHtml::passwordField('newpassword', '', array('placeholder' => 'New Password')); ?><br />
			<div class="row-fluid">
				<div class="span2">
					<div id="passmeter" class="progress">	<!--  progress-striped -->
						<div class="bar" style="width: 0%;"></div>
					</div>
				</div>
			</div>
			
			<?php echo CHtml::passwordField('newpassword_confirm', '', array('placeholder' => 'Confirm Password')); ?><br />
			
			<label class="checkbox">
				<?php echo CHtml::checkBox('change_mysql_pass', true, array('value' => 1)); ?>
				Change the MySQL password.
			</label>
			<button class="btn btn-small btn-primary" id="change_passwords_btn" type="button" data-loading-text="Updating...">Change Passwords</button><br /><br />
			<div class="row-fluid">
				<div class="span12 hide" id="cpanelpasswords_info">
					<div class="alert alter-block alert-info">
			<!--			<button type="button" class="close" data-dismiss="alert">&times;</button>-->
						<div class="content"></div>
					</div>
				</div>
			</div>
			<script>
				$('#newpassword').keyup(function(e) {
					var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
					var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
					var weakRegex = new RegExp("(?=.{6,}).*", "g");
	
					var password = $(this).val();
					$('#passmeter .bar').attr('class', 'bar');
					if(password.length < 3){
						$('#passmeter .bar').css("width","2%");
						$('#passmeter .bar').addClass('bar-danger');
					}else if(false == weakRegex.test(password)){
						$('#passmeter .bar').css("width","25%");
						$('#passmeter .bar').addClass('bar-danger');
					}else if(strongRegex.test(password)){
						$('#passmeter .bar').css("width","100%");
						$('#passmeter .bar').addClass('bar-success');
					}else if(mediumRegex.test(password)){
						$('#passmeter .bar').css("width","50%");
						$('#passmeter .bar').removeClass('bar-warning');
					}else{
						$('#passmeter .bar').css("width","2%");
						$('#passmeter .bar').addClass('bar-danger');
					}
				});

				function checkPassword()
				{
					var password = $('#newpassword').val();
					var newpassword_confirm = $('#newpassword_confirm').val();
					var error = '';

					if(password.length < 5){
						error = 'Sorry, passwords must be at least 5 characters for security reasons.';
					}else if(password != newpassword_confirm){
						error = 'Please confirm password.';
					}
					if(error != ''){
						$('#cpanelpasswords_info').blink({
							'beforeCall': function(){
								$('#cpanelpasswords_info .content').append(
									'<p><b>'+error+'</b></p>'
								);
							},
							'showTime': 0
						});

						return false;
					}
					
					return true;
				}
				
				var changepassRequest = function(){
					this.requestUrl = '';
					this.id_cpanel_accounts = null;
					this.newpass = null;
					this.change_mysql_pass = 0;

					this.beforeRequest = function(id_cpanel_account, doTimes){};
					this.requestCallback = function(id_cpanel_account, serverReturn, doTimes){};
					this.afterDone = function(){};

					this.doTimes = 0;
					this.do = function(){
						this.doTimes++;
						var id_cpanel_account = this.id_cpanel_accounts.pop();
						if(!id_cpanel_account){
							this.afterDone();
							return false;
						}
						this.beforeRequest(id_cpanel_account, this.doTimes);
						var thisObj = this;
						$.ajax({
							type: 'post',
							url: thisObj.requestUrl,
//							data: $.extend( 
//								{id_account: id_account},
//								thisObj.services
//							),
							data: {
								id_cpanel_account: id_cpanel_account,
								newpass: thisObj.newpass,
								change_mysql_pass: thisObj.change_mysql_pass
							},
							success: function(ret){
								thisObj.requestCallback(id_cpanel_account, ret, thisObj.doTimes);
								thisObj.do();
								return true;
							},
							dataType: 'json'
						});
					};
				};
				
				$('#change_passwords_btn').click(function(){
					if(!checkPassword()){
						return false;
					}

					var password = $('#newpassword').val();
					var change_mysql_pass = $('#change_mysql_pass').is(':checked')? 1 : 0;
					var id_cpanel_accounts = [];
					if($('#id_cpanel_accounts').val() == ''){
						$('#id_cpanel_accounts option[value!=""]').each(function(k, v){
							id_cpanel_accounts.push($(v).val());
						});
					}else{
						id_cpanel_accounts.push($('#id_cpanel_accounts').val());
					}

					$('#cpanelpasswords_info').blink({
						'beforeCall': function(){
							$('#cpanelpasswords_info .content').append(
								'<p><b>Status ('+id_cpanel_accounts.length+' Account)</b></p>'
							);
						},
						'callBack': function(){
							var changepassRequester = new changepassRequest();
							var length = id_cpanel_accounts.length;
							changepassRequester.newpass = password;
							changepassRequester.id_cpanel_accounts = id_cpanel_accounts;
							changepassRequester.change_mysql_pass = change_mysql_pass;

							changepassRequester.requestUrl = <?php echo json_encode(url('whm/cpanelaccount/passwords')); ?>;
							changepassRequester.beforeRequest = function(id_cpanel_account, doTimes){
								$('#change_passwords_btn').button('loading');
								$('#cpanelpasswords_info .content').append(
									'<p>'+$('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text()+': Waiting...'+' ('+doTimes+' of '+length+' Accounts)</p>'
								);
							};
							changepassRequester.afterDone = function(){
								$('#change_passwords_btn').button('reset');
							}
							changepassRequester.requestCallback = function(id_cpanel_account, serverReturn, doTimes){
								if(serverReturn.result){
									var username = $('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text();
									if(serverReturn.passwd.status == 1){
										var result = '<p class="text-success accordion-toggle" data-toggle="collapse" data-target="#'+id_cpanel_account+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+username+' - Success</b> Show/Hide Raw Output</p>';
									}else{
										var result = '<p class="text-error accordion-toggle" data-toggle="collapse" data-target="#'+id_cpanel_account+serverReturn.time+'">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+username+' - Failed</b> Show/Hide Raw Output</p>';
									}
									$('#cpanelpasswords_info .content').append(result);
									$('#cpanelpasswords_info .content').append('<p id="'+id_cpanel_account+serverReturn.time+'" class="muted collapse out">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>'+($.isEmptyObject(serverReturn.passwd.rawout)? serverReturn.passwd.statusmsg : serverReturn.passwd.rawout)+'</b></p>');
								}else{
									$.each(serverReturn.errors, function(attrName, error){
										$(error).each(function(k, e){
											$('#cpanelpasswords_info .content').append(
												'<p class="text-error">&nbsp;&nbsp;&nbsp;&nbsp; <b>'+$('#id_cpanel_accounts option[value="'+id_cpanel_account+'"]').text()+' - '+e+' ('+doTimes+' of '+length+' Accounts)</b></p>'
											);
										});
									});
								}
							};
							changepassRequester.do();
						},
						'showTime': 0
					});
					
				});
			</script>
		</fieldset>
		
	</div>
</div>